;** the Midi workspace definition **

; scheduler time and value struct
; There is a block of 1024 of these allocated in the workspace below
; The time is the schedule time by the millisecond clock
; The value is the r1 parameter to MIDI_TxCommand
                  ^  0
ScheduleTime      #  4
ScheduleValue     #  4
SchedulerSlotSize #  0

; The workspace definition

                ^  0   ; set assembler memory var @ to 0
; this must be at the start of the workspace
csRoutineAddr   #  4 ; address of centisecond routine to pass to sound dma for sirq

; permit 4 Ports maximum => 4 hardware addresses
; these should be here at the start of the workspace + 1 word for
; access via the workspace pointer from another (address log) rm
PoduleBase      #  4*4 ; this is stored here in Initialise_Module
UARTOffset      #  4*4 ; offset from podule base address of acia or uart
UARTbase        #  4*4 ; base address of acia or uart

Sound_QTick     #  4 ; tick entry to sound scheduler normally used by sound dma rm.

RxBufferLen     #  4 ; size of receive buffer (programmable)

; pointers to buffers claimed separately from workspace
TxBufferStarts      #  4*4
RxBufferStarts      #  4*4
RxTimesBufferStarts #  4*4

SystemResetClearStart # 0

IMRCurrent      #  4 ; current programmed value of interrupt mask reg in UART

; buffer pointers: in = ms16b, out = ls16b.
RxBufferPntrs   #  4*4 ; 4 receive FIFO buffer input and output pointers
TxBufferPntrs   #  4*4 ; 4 transmit FIFO buffer input and output pointers

; Workspace.
ErrorFlag       #  4  ; various routines can set this flag to indicate that an
                      ; error has occurred, and what type
OverflowPort    #  4  ; bits 0..3 set to indicate which port has had an
                      ; unreported background buffer overflow error
SendStatus      #  4  ; the running status (voice & mode only) of the
                      ; transmitted midi data
StatusAge       #  4  ; the age of the running status so it can be updated
                      ; when it reaches MaxRunningStatusAge
TxInactiveTime  #  4  ; time since last MIDI byte sent (in centiseconds)
                      ; for active sensing monitor.
RxStatus        #  4  ; Running status of received data (for RxCommand)
RxByteCounter   #  4  ; bits 0-3 number of bytes Rx'd since status or exec
                      ; bits 4-7 number of bytes expected for current status
RxByte1         #  4  ; first received data byte after status
TxChannel       #  4  ; channel no. (0..15) that MIDI data is currently sent on
SongPosPointer  #  4  ; This is incremented on every TimingClock
                      ; message received, reset and started on start, stopped
                      ; on stop, and set on SongPositionPointer messages.
                      ; It is divided by 6 to give true SPP value.

; the following 3 words should be together to allow ldm of them
; by the ms routine
ms_count        #  4  ; hi-resolution timestamp, incremented every
                      ; millisecond
tc_rate         #  4  ; value for ms_count to reach before op of TC
                      ; and incrementing value in top 16 bits
ModeFlagBits    #  4  ; set of bits determining timing mode, MIDI mode, etc
next_schedule_t #  4  ; Copy of next schedule time for quick load
                      ; and compare with ms_count.

LastBeat        #  4  ; Last value and incrementing value of sound
                      ; system beat counter for Timing Clock transmission
BarLength       #  4  ; Current value of sound sys BEATS. This enables
                      ; us to check for bar length change, and reset bar
                      ; counter to 0 
Tempo           #  4  ; current value of sound sys TEMPO
CurrentQBeat    #  4  ; This is the current value of the tempo beat
               ; counter. Bar counter is top 2 bytes, beat counter is bottom 2
IgnoreState     #  4  ; Define which received messages to ignore

SPPlsb          #  4  ; this is used to store the lsb of a Song Position
                      ; Pointer message, while awaiting the msb
SPPchannel      #  4  ; which Port is the current SPP being received by (0-3)?

TimeCount       #  4  ; Decremented every centisecond. After SlowClock it does
                    ; those things which need doing regularly, but not every cs

ClearSpaceStart *  @ ; workspace to clear on SWI MIDI_SoundEnable

; workspace for MIDI control of sound system
InterpretChannel # 4  ; which Port buffers is the interpreter looking at (0-3)?
VoiceStates     #  4  ; 1 bit per note to account for ons and offs
VoiceMPitches   #  8  ; 1 byte of midi pitch per voice
VoiceSPitches   #  32 ; 1 word (32b) of pitch (XSound_Control value)
                      ; per voice. Note: the size of this could be halved.
VoiceAmps       #  8  ; 1 byte of XSound_Control amplitude value per voice.
                      ; Note. Bit 8 should also be set when using.
BufferSoundOutPntr # 4  ; sound interpreter getbyte pointer
RunningStatus   #  1  ; the current status of the midi stream (for sound interpreter)
FirstDataByte   #  1  ; the value of the second byte for 3-byte instructions
ByteCounter     #  1  ; bits 0-3 number of bytes Rx'd since status or exec
                      ; bits 4-7 number of bytes expected for current status
PendSysMsg      #  1  ; bit 1 = 1 if pending system message (SPP or SSel status received)
CurrentChannel  #  2  ; current MIDI channel for omni off channel check
MonoChannels    #  2  ; M = number of channels in Omni-off/Mono mode

ClearSpaceEnd   *  @

RxInactiveTime  #  4  ; time since last MIDI byte received (in centiseconds)
                      ; for active sensing monitors.
SoundConfiguration # 4 ; number of configured channels before
                       ; SWI MIDI_SoundEnable was called
Tuning          #  4  ; Sound system tuning parameter

ScratchBuffer   #  4  ; used for os_gstrans op buffer for * commands

;maxirqtime # 4 ; max irq time recorded so far
;irqtime    # 4 ; time of last irq in timer ticks
;irqnotserviced # 4 ; time taken to discover that this irq was not generated by midi
;thistime   # 4

SystemResetClearEnd  # 0

extrabit   # &4   ; this aligns buffers to &100 address and thus they are not
                  ; out of immediate value address range
; midi command scheduler workspace

MaxSchedulerSlots * 1024

; define an array of structs.. not very efficient in memory usage, but speed is
; very important
Slot_p          #  4  ; pointers to next free (top 16) and next to empty
                      ; (bottom 16) scheduler slots.

SchedulerSpace  # MaxSchedulerSlots * SchedulerSlotSize

WorkSpace * @

 ! 0, "Workspace size ":CC::STR:@

      END
